package com.cn.demo.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 线程池的配置
 *
 * @author wangjf
 * @version 1.0
 * @createDate 2019/12/6 14:49
 * @projectName demo
 */
@Configuration
@EnableAsync
@Slf4j
public class ThreadPoolExecutorConfig {


    /**
     * 功能描述: 生产者线程池配置
     *
     * @param
     * @return java.util.concurrent.Executor
     * @author: wangjf
     * @createTime: 2019/12/6 14:54
     */
    @Bean("poolProduce")
    public Executor poolProduce() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(100000);
        executor.setThreadNamePrefix("poolProduce-");
        executor.initialize();
        return executor;
    }

    /**
     * 功能描述: 消费线程池
     *
     * @param
     * @return java.util.concurrent.Executor
     * @author: wangjf
     * @createTime: 2019/12/6 15:16
     */
    @Bean("poolConsumer")
    public Executor poolConsumer() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2);
        executor.setMaxPoolSize(100);
        executor.setQueueCapacity(100000);
        executor.setThreadNamePrefix("poolConsumer-");
        executor.initialize();
        return executor;
    }
}
